﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;

using Module;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        LoreView.aspx.cs           
/// Author :         wangbc
/// CDT:             2012-02-01           
/// Version:         1.0.0.0                
/// Depiction:       知识库详细
/// </summary>
public partial class main_admin_LoreView : UserControl.CommonPage
{
    /// <summary>
    /// 知识库EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 知识库Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    /// 知识库Entity名字
    /// </summary>    
    private const string EM_FULL_NAME = WEBConstants.EM_LORE_MANAGE;

    //知识库ID
    public string chkID = "";

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //知识库ID
        chkID = (string)Request.QueryString.Get("chkID");
        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];

        em = CDEEMFactory.getEMByFullName(EM_FULL_NAME);
        if (!Page.IsPostBack)
        {
            //ID不为空修改知识库，否则新增知识库
            if (chkID != "" && chkID != null)
            {
                //修改
                this.page_title.InnerText = "知识库详细";
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("ID", chkID);
                List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
                //知识库ID是否存在
                if (listEntity.Count > 0)
                {
                    m_entity = listEntity[0];                   

                    //查看记录
                    loreOperate(chkID,op.SysID,"1");
                }
                else
                {
                    m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                    m_entity.Operation = CDEDataOperation.INSERT;
                }
            }
            else
            {
                //新增
                this.page_title.InnerText = "知识库新增";
                m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                m_entity.Operation = CDEDataOperation.INSERT;
                
            }
            Session["MENTITY"] = m_entity;
            //知识库信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            m_entity = (CDEEntity)Session["MENTITY"];
            //页面信息放入知识库Entity
            this.setEntityFromRequest(m_entity, Request.Params);
        }

        //返回显示
        string viewType = Util.objectToString((string)Request.QueryString.Get("viewType"));
        if (viewType != "")
        {
            this.Button3.Visible = false;
        }
        else
        {
            this.Button3.Visible = true;
        }
    }


  
    /// <summary>
    /// 知识库操作
    /// </summary>
    /// <param name="id"></param>
    /// <param name="user_id"></param>
    /// <param name="type">0:下载,1:查看</param>
    /// <returns></returns>
    public bool loreOperate(string id,string user_id,string type) {

        //每天只记录一次
        StringBuilder sql = new StringBuilder();
        sql.AppendFormat(@"select id from LORE_OPERATE where lore_id ={0} and user_id={1} and    oper_type={2} and
                            dbo.DATE_FORMAT(insert_date,'YYYY/MM/DD')=dbo.DATE_FORMAT(getdate(),'YYYY/MM/DD')
                            ",id,user_id,type);
        DataTable op_table=Util.loadSql(sql.ToString());
        if (op_table.Rows.Count!=0) {
            return false;
        }

        //操作明细记录
        CDEEntityManager oper_em = CDEEMFactory.getEMByFullName("WEB.LORE_OPERATE");
        CDEEntity oper_entity = CDEEMServiceFactory.getEMService(oper_em).CreateEntity();
        oper_entity.Operation = CDEDataOperation.INSERT;
        oper_entity.set("LORE_ID",id);
        oper_entity.set("USER_ID", user_id);
        oper_entity.set("OPER_TYPE", type);
        List<CDEEntity> listEntity = new List<CDEEntity>();
        listEntity.Add(oper_entity);
        CDEService.UpdateEntities(oper_em, listEntity);
         if (listEntity[0].Result == CDEDataOperationResult.ERROR)
         {
             return false;
         }
         else
         {
             //知识库主表查看数更新
             sql = new StringBuilder();
             sql.AppendFormat(@"update LORE set VIEWS=VIEWS+1 where id={0}", id);
             Util.ExcSql(sql.ToString());

             return true;
         }
    }

    /// <summary>
    /// 返回按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        if (Sel_MenuNode == null)
            return;
        StringBuilder url = new StringBuilder();
        url.Append("../admin/LoreListView.aspx?EM=");
        url.Append(EM_FULL_NAME);
        url.Append("&sel_menu_id=");
        url.Append(Sel_MenuNode.Attributes["id"].Value);
        url.Append("&IsBackSc=1");
        Response.Redirect(url.ToString());

    }
}
